var app = getApp();
var API_SERVER = 'https://wwdh.dingdangzc.com/';
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':')
}

/**
 * 时间戳转化为年 月 日 时 分 秒
 * number: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
function formatDivTime(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('.')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//高德
var GaoName = {
  shop_coordinate: 'https://restapi.amap.com/v3/geocode/geo',
  GaodeKey: '4fefd313d8b4c97763acd8928bf62ad9' //高德Key
}

var URL = {
  login: API_SERVER + 'api/public/wxlogin',
  phone_code: API_SERVER + 'api/public/getPhoneCode', //手机验证码
  phone_bind: API_SERVER + 'api/public/bindPhone', //绑定手机号
  goods_list: API_SERVER + 'api/goods/goods_list', //商品列表
  goods_detail: API_SERVER + 'api/goods/goods_detail', //商品详情 商品ID(id)
  goods_pay: API_SERVER + 'api/Wxpay/pay', //支付
  order_list: API_SERVER + 'api/order/order_list', //订单列表 用户ID(openid)
  order_detail: API_SERVER + 'api/order/order_detail', //订单详情 订单ID(id)
}

/**
 * 封装 http 函数，默认‘GET’ 提交
 */
function POST(url, parames, success, fail) {
  wx.request({
    url: url,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded;'
    },
    data: parames,
    success: function(res) {
      // console.log(res)
      if (res.data.Status == 200 || res.data.return == "true") {
        success(res.data);
      } else {
        fail({
          msg: res.data.retinfo,
          status: res.data.Status
        });
      }
    },
    fail: function(res) {
      //转译
      fail({
        msg: '请求超时，请检查网络后重试！',
        status: 999
      });
    }
  })
}

/**
 * 封装 http 函数，默认‘GET’ 提交
 */
function GET(url, success, fail) {
  // console.log('====GET====' + url)
  wx.request({
    url: url,
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/x-www-form-urlencoded;'
    }, // 设置请求的 header
    success: function(res) {
      // console.log(res)
      if (res.data.Status == 200 || res.data.return == "true") {
        success(res.data);
      } else {
        fail({
          msg: res.data.retinfo,
          status: res.data.Status
        });
      }
    },
    fail: function() {
      //转译
      fail({
        msg: '请求超时，请检查网络后重试！',
        status: 999
      });
    }
  })
}

function getOrganKey(success, fail) {
  if (app.authCode) {
    success(app.authCode);
  } else {
    wx.showLoading({
      title: '授权中',
    });
    var paras = {};
    paras.Command = 'Employee/GetAuthCode';
    paras.Params = JSON.stringify({
      organKey: '2102c01b346a4a4d4e5905381024cb49'
    });

    POST(URL.bmsEmployee, paras, function(sData) {
      wx.hideLoading();
      var aObj = JSON.parse(sData.JSON);
      app.authCode = aObj.AuthCode;
      success(app.authCode);
    }, function(eData) {
      wx.hideLoading();
      fail(eData);
    });
  }
}

var user = undefined

module.exports = {
  formatDivTime: formatDivTime,
  formatTime: formatTime,
  formatDate: formatDate,
  POST: POST,
  URL: URL,
  GET: GET,
  getOrganKey: getOrganKey,
  GaoName: GaoName, //高德相关
  user:user
}